﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using JinJiang.BookingEngine.Services.Notification.Mailing;
using JinJiang.BookingEngine.Services.Notification.Sms;

namespace JinJiang.BookingEngine.Services
{
    public static class NotificationService
    {
        private static readonly string s_User;
        private static readonly string s_Password;

        static NotificationService()
        {
            s_User = string.IsNullOrEmpty(ConfigurationManager.AppSettings["NotificationUserName"]) ? "JINJIANGBOOKINGENGINE" : ConfigurationManager.AppSettings["NotificationUserName"];
            s_Password = string.IsNullOrEmpty(ConfigurationManager.AppSettings["NotificationPassword"]) ? "jj_h4dk9776" : ConfigurationManager.AppSettings["NotificationPassword"];
        }

        #region 发送邮件

        /// <summary>
        /// </summary>
        /// <param name="title"> </param>
        /// <param name="content"> </param>
        /// <param name="cnfnum"> </param>
        /// <param name="receiverAddresses"> </param>
        /// <returns> </returns>
        public static bool SendMail(string title, string content, string cnfnum, List<string> receiverAddresses)
        {
            try
            {
                using (var serviceClient = new MailWebServiceClient())
                {
                    var mailRq = new mailRQ
                                     {
                                         syscode = s_User,
                                         sysuser = s_User,
                                         syspasswd = s_Password,
                                         bizcode = "BookingEngine",
                                         cnfnum = cnfnum,
                                         mailtitle = title,
                                         mailmsg = content,
                                         receivemails = receiverAddresses.ToArray()
                                     };
                    var result = serviceClient.sendHtmlMail(mailRq);
                    if (result == null || !result.success)
                    {
                        return false;
                    }
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return false;
            }
        }

        #endregion

        #region 发送短信

        public static bool SendSms(string content, string cnfnum, List<string> receiverNumbers)
        {
            try
            {
                using (var serviceClient = new SmsWebServiceClient())
                {
                    var smsRq = new smsRQ
                                    {
                                        syscode = s_User,
                                        sysuser = s_User,
                                        syspasswd = s_Password,
                                        bizcode = "BookingEngine",
                                        cnfnum = cnfnum,
                                        msg = content,
                                        moblies = receiverNumbers.ToArray(),
                                    };
                    var result = serviceClient.sendSms(smsRq);
                    if (result == null || !result.success)
                    {
                        return false;
                    }
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return false;
            }
        }

        #endregion
    }
}